package com.jhy.guavacase.utilites;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author jhy
 * @ClassName JoinerCase.java
 * @Description Guava的连接器Guava
 * @createTime 2022年01月27日 11:16:00
 */
public class JoinerCase {


    //1.最基本的连接示例
    @Test
    public void joinerQuickStart(){
        List<String> stringList = Arrays.asList("java","spring","kafka");
        String res = Joiner.on("=").join(stringList);
        Assertions.assertEquals(res, "java=spring=kafka");
    }

    //2.集合中Null导致空指针异常
    @Test
    public void joinerNPE(){
        List<String> stringList = Arrays.asList("java","spring","kafka",null);
        try{
            String res = Joiner.on("=").join(stringList);
        }catch (NullPointerException e){
            System.out.println("空指针异常");
        }
    }

    //3.忽略集合中的null
    @Test
    public void joinerSkipNulls(){
        List<String> stringList = Arrays.asList("java","spring",null,"kafka");
        String res = Joiner.on("=").skipNulls().join(stringList);
        Assertions.assertEquals(res, "java=spring=kafka");
    }

    //4.用默认值代替集合中的null
    @Test
    public void joinerUseForNull(){
        List<String> stringList = Arrays.asList("java","spring",null,"kafka");
        String res = Joiner.on("=").useForNull("default_value").join(stringList);
        Assertions.assertEquals(res, "java=spring=default_value=kafka");
    }

    //5.添加到本地文件中
    @Test
    public void joinerAppendToWriter() throws IOException {
        FileWriter fileWriter = new FileWriter(new File("E://file.txt"));
        Joiner.on("=").appendTo(fileWriter, Arrays.asList("java", "spring", "kafka"));
        fileWriter.close();
    }

    //6.连接Map中的key和value
    @Test
    public void joinerMap(){
        ImmutableMap<String, String> map = ImmutableMap.of("java", "后端", "javascript", "前端");
        //用=连接map中的Entry，用->连接每个entry的key和value
        String res = Joiner.on("=").withKeyValueSeparator("->").join(map);
        Assertions.assertEquals(res, "java->后端=javascript->前端");
    }

    //7.使用Stream流进行拼接
    @Test
    public void streamToJoin(){
        List<String> stringList = Arrays.asList("java", "spring", null,"kafka");
        String res = stringList.stream().filter(item->item != null).collect(Collectors.joining("="));
        Assertions.assertEquals(res, "java=spring=kafka");
    }

    //8.MapJoiner对于Map中的null无能为力-->空指针异常
    @Test
    public void MapJoinerNPE(){
        HashMap<String, String> map = new HashMap<>();
        map.put("java","后端");
        map.put(null,null);
        map.put("javaScript","前端");
        String res = Joiner.on("=").withKeyValueSeparator("->").join(map);
    }

    //9.使用StringBuilder连接有null的Map
    @Test
    public void userStringBuilderSolveNullMap(){
        //Entry之间的分割符
        String separator = "->";
        //每个Entry中key和value的分割符
        String keyValueSeparator = ":";

        HashMap<String, String> map = new HashMap<>();
        map.put(null,"aaa");
        map.put("aaa",null);
        map.put("java","后端");
        map.put(null,null);
        map.put("javaScript","前端");

        StringBuilder sb = new StringBuilder();
        Iterator<String> iterator = map.keySet().iterator();

        //append第一个不为null的元素
        while (iterator.hasNext()){
            String next = iterator.next();
            if (next != null && map.get(next)!=null) {
                sb.append(next);
                sb.append(keyValueSeparator);
                sb.append(map.get(next));
                break;
            }
        }

        //依次append元素和连接符
        while (iterator.hasNext()){
            String next = iterator.next();
            if (next != null && map.get(next)!=null) {
                sb.append(separator);
                sb.append(next);
                sb.append(keyValueSeparator);
                sb.append(map.get(next));
            }
        }
        Assertions.assertEquals(sb.toString(),"java:后端->javaScript:前端");

    }




}
